﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.InterferenceVolume.CApIVInterferenceVolumeCreator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.InterferenceVolume;

internal class CApIVInterferenceVolumeCreator
{
  private IUiIVInterferenceVolumeDataProvider m_dataProvider;
  private TxInterferenceVolume_MayProceedCalculation m_mayProceedCallback;
  private CUiIVNotificationFeedbackEventHandler m_notificationFeedback;
  private bool m_isCreationRunning;
  private bool m_isCreationAbortSubmitted;
  private CApIVInterferenceVolumeCreationParams m_creationParams;

  internal event CApIVInterferenceVolumeCreator.CreateStartedEventHandler CreateStarted;

  internal event CApIVInterferenceVolumeCreator.CreateFinishedEventHandler CreateFinished;

  internal CApIVInterferenceVolumeCreator()
  {
    // ISSUE: method pointer
    this.m_mayProceedCallback = (TxInterferenceVolume_MayProceedCalculation) Delegate.Combine((Delegate) this.m_mayProceedCallback, (Delegate) new TxInterferenceVolume_MayProceedCalculation((object) this, __methodptr(MayProceed)));
  }

  internal CApIVInterferenceVolumeCreator(IUiIVInterferenceVolumeDataProvider dataProvider)
  {
    this.m_dataProvider = dataProvider;
    // ISSUE: method pointer
    this.m_mayProceedCallback = (TxInterferenceVolume_MayProceedCalculation) Delegate.Combine((Delegate) this.m_mayProceedCallback, (Delegate) new TxInterferenceVolume_MayProceedCalculation((object) this, __methodptr(MayProceed)));
  }

  internal IUiIVInterferenceVolumeDataProvider DataProvider
  {
    get => this.m_dataProvider;
    set => this.m_dataProvider = value;
  }

  internal CUiIVNotificationFeedbackEventHandler NotificationFeedback
  {
    set => this.m_notificationFeedback = value;
  }

  internal TxInterferenceVolume_MayProceedCalculation CallBack
  {
    set => this.m_mayProceedCallback = value;
  }

  internal bool IsCreationRunning => this.m_isCreationRunning;

  internal bool IsCreationAbortSubmitted => this.m_isCreationAbortSubmitted;

  internal bool SweptVolumes_AreInitialized()
  {
    bool flag = false;
    TxObjectList dataSweptVolumes = this.m_dataProvider.IVData_SweptVolumes;
    if (dataSweptVolumes != null && ((Collection<ITxObject>) dataSweptVolumes).Count == 2)
      flag = true;
    return flag;
  }

  internal bool SweptVolumes_AreIdentical()
  {
    bool flag = false;
    if (this.SweptVolumes_AreInitialized())
    {
      TxObjectList dataSweptVolumes = this.m_dataProvider.IVData_SweptVolumes;
      flag = ((Collection<ITxObject>) dataSweptVolumes)[0] == ((Collection<ITxObject>) dataSweptVolumes)[1];
    }
    return flag;
  }

  internal bool SweptVolumes_HaveIdenticalHoldingObject()
  {
    bool flag = false;
    if (this.SweptVolumes_AreInitialized())
    {
      TxObjectList dataSweptVolumes = this.m_dataProvider.IVData_SweptVolumes;
      ITxSweptVolumeHoldingObject volumeHoldingObject1 = CApIVUtilities.GetSweptVolumeHoldingObject(((Collection<ITxObject>) dataSweptVolumes)[0] as TxSweptVolume);
      ITxSweptVolumeHoldingObject volumeHoldingObject2 = CApIVUtilities.GetSweptVolumeHoldingObject(((Collection<ITxObject>) dataSweptVolumes)[1] as TxSweptVolume);
      if (volumeHoldingObject1 != null && volumeHoldingObject1 == volumeHoldingObject2)
        flag = true;
    }
    return flag;
  }

  internal TxInterferenceVolume SweptVolumes_ExistingInterferenceVolume()
  {
    TxInterferenceVolume interferenceVolume = (TxInterferenceVolume) null;
    if (this.SweptVolumes_AreInitialized())
      interferenceVolume = CApIVUtilities.GetExistingInterferenceVolume(this.m_dataProvider.IVData_SweptVolumes);
    return interferenceVolume;
  }

  internal bool SweptVolumes_ExistEmptySweptVolumes()
  {
    bool flag = false;
    if (this.SweptVolumes_AreInitialized())
    {
      TxObjectList dataSweptVolumes = this.m_dataProvider.IVData_SweptVolumes;
      flag = CApIVUtilities.IsEmptySweptVolume(((Collection<ITxObject>) dataSweptVolumes)[0] as TxSweptVolume);
      if (!flag)
        flag = CApIVUtilities.IsEmptySweptVolume(((Collection<ITxObject>) dataSweptVolumes)[1] as TxSweptVolume);
    }
    return flag;
  }

  internal bool SweptVolumes_ExistBlankedSweptVolumes()
  {
    bool flag = false;
    if (this.SweptVolumes_AreInitialized())
    {
      TxObjectList dataSweptVolumes = this.m_dataProvider.IVData_SweptVolumes;
      flag = (((Collection<ITxObject>) dataSweptVolumes)[0] as ITxDisplayableObject).Visibility == 2;
      if (!flag)
        flag = (((Collection<ITxObject>) dataSweptVolumes)[1] as ITxDisplayableObject).Visibility == 2;
    }
    return flag;
  }

  internal bool InterferenceVolumeName_IsValid()
  {
    bool flag = false;
    if (this.m_dataProvider != null)
    {
      string ivDataName = this.m_dataProvider.IVData_Name;
      flag = ivDataName != null && ivDataName.Length > 0;
    }
    return flag;
  }

  internal bool CanCreate(out CUiIVNotification notification)
  {
    bool flag = false;
    notification = (CUiIVNotification) null;
    if (!this.IsCreationRunning && this.SweptVolumes_AreInitialized() && this.InterferenceVolumeName_IsValid())
    {
      if (this.SweptVolumes_AreIdentical())
        notification = this.GetNotification_IdenticalSweptVolumes();
      else if (this.SweptVolumes_HaveIdenticalHoldingObject())
        notification = this.GetNotification_IdenticalSweptVolumesHoldingObject();
      else
        flag = true;
    }
    return flag;
  }

  internal bool Create()
  {
    bool flag1 = false;
    CUiIVNotification notification = (CUiIVNotification) null;
    if (!this.CanCreate(out notification))
    {
      if (notification != null)
      {
        int num = (int) this.Fire_NotificationFeedback(notification);
      }
    }
    else
    {
      bool flag2 = true;
      this.m_creationParams = new CApIVInterferenceVolumeCreationParams(this.m_dataProvider.IVData_Name, this.m_dataProvider.IVData_Color, this.m_dataProvider.IVData_SweptVolumes);
      TxInterferenceVolume interferenceVolume = this.SweptVolumes_ExistingInterferenceVolume();
      if (TxInterferenceVolume.op_Inequality(interferenceVolume, (ITxObject) null))
      {
        flag2 = this.Fire_NotificationFeedback(this.GetNotification_ExistingInterferenceVolume()) != CUiIVNotificationFeedbackEventArgs.eFeedbackResult.Abort;
        if (flag2)
          this.m_creationParams.ExistingInterferenceVolume = interferenceVolume;
      }
      if (flag2 && this.SweptVolumes_ExistEmptySweptVolumes())
      {
        flag2 = this.Fire_NotificationFeedback(this.GetNotification_EmptyInterferenceVolumes()) != CUiIVNotificationFeedbackEventArgs.eFeedbackResult.Abort;
        if (flag2)
          this.m_creationParams.InsertAction(CApIVInterferenceVolumeCreationParams.eActions.SkipVolumeCalculation);
      }
      if (flag2 && !this.m_creationParams.RequireAction(CApIVInterferenceVolumeCreationParams.eActions.SkipVolumeCalculation) && this.SweptVolumes_ExistBlankedSweptVolumes())
      {
        flag2 = this.Fire_NotificationFeedback(this.GetNotification_BlankedSweptVolumes()) != CUiIVNotificationFeedbackEventArgs.eFeedbackResult.Abort;
        if (flag2)
          this.m_creationParams.InsertAction(CApIVInterferenceVolumeCreationParams.eActions.DisplayBlankedSweptVolumes);
      }
      if (flag2)
        flag1 = this.StartCreation();
    }
    return flag1;
  }

  private bool StartCreation()
  {
    bool flag = false;
    if (this.m_creationParams != null)
    {
      try
      {
        flag = !TxInterferenceVolume.op_Inequality(this.m_creationParams.ExistingInterferenceVolume, (ITxObject) null) ? this.CreateNewInterferenceVolume() : this.UpdateInterferenceVolume();
      }
      finally
      {
        if (this.IsCreationRunning)
          this.PerformPostCreateActions();
      }
    }
    return flag;
  }

  internal bool AbortCreation()
  {
    bool flag = false;
    if (this.IsCreationRunning)
    {
      this.m_isCreationAbortSubmitted = true;
      flag = true;
    }
    return flag;
  }

  private bool CreateNewInterferenceVolume()
  {
    bool interferenceVolume1 = false;
    if (this.m_creationParams != null)
    {
      this.PerformStartCreateActions();
      TxInterferenceVolume interferenceVolume2 = this.PerformCreation(this.GenerateCreationData());
      if (TxInterferenceVolume.op_Inequality(interferenceVolume2, (ITxObject) null))
      {
        if (!this.m_creationParams.RequireAction(CApIVInterferenceVolumeCreationParams.eActions.SkipVolumeCalculation))
        {
          TxInterferenceVolumeCalculationData resultData = (TxInterferenceVolumeCalculationData) null;
          this.GenerateCalculationData(ref resultData);
          interferenceVolume1 = this.PerformCalculation(interferenceVolume2, resultData);
        }
        else
          interferenceVolume1 = true;
        if (!interferenceVolume1)
          interferenceVolume2.Delete();
        else if (this.m_creationParams.RequireAction(CApIVInterferenceVolumeCreationParams.eActions.DeleteEmptyInterferenceVolume))
        {
          interferenceVolume2.Delete();
        }
        else
        {
          CApIVUtilities.SetColor((ITxDisplayableObject) interferenceVolume2, this.m_creationParams.Color);
          interferenceVolume2.Lock();
        }
      }
      TxApplication.ActiveUndoManager.ClearAllTransactions();
      this.PerformPostCreateActions();
    }
    return interferenceVolume1;
  }

  private bool UpdateInterferenceVolume()
  {
    bool flag = false;
    if (this.m_creationParams != null && TxInterferenceVolume.op_Inequality(this.m_creationParams.ExistingInterferenceVolume, (ITxObject) null))
    {
      TxInterferenceVolume interferenceVolume = this.m_creationParams.ExistingInterferenceVolume;
      this.PerformStartCreateActions();
      string name = interferenceVolume.Name;
      TxColor color = CApIVUtilities.GetColor((ITxDisplayableObject) interferenceVolume);
      interferenceVolume.Unlock();
      interferenceVolume.Name = this.m_creationParams.Name;
      interferenceVolume.Clear();
      TxApplication.ActiveUndoManager.ClearAllTransactions();
      if (!this.m_creationParams.RequireAction(CApIVInterferenceVolumeCreationParams.eActions.SkipVolumeCalculation))
      {
        TxInterferenceVolumeCalculationData resultData = (TxInterferenceVolumeCalculationData) null;
        this.GenerateCalculationData(ref resultData);
        flag = this.PerformCalculation(interferenceVolume, resultData);
      }
      else
        flag = true;
      if (!flag)
      {
        interferenceVolume.Rollback();
        interferenceVolume.Name = name;
        if (TxColor.op_Inequality(color, (TxColor) null))
          CApIVUtilities.SetColor((ITxDisplayableObject) interferenceVolume, color);
      }
      else if (this.m_creationParams.RequireAction(CApIVInterferenceVolumeCreationParams.eActions.DeleteEmptyInterferenceVolume))
      {
        interferenceVolume.Delete();
      }
      else
      {
        CApIVUtilities.SetColor((ITxDisplayableObject) interferenceVolume, this.m_creationParams.Color);
        interferenceVolume.Lock();
      }
      TxApplication.ActiveUndoManager.ClearAllTransactions();
      this.PerformPostCreateActions();
    }
    return flag;
  }

  private void PerformStartCreateActions()
  {
    this.m_isCreationAbortSubmitted = false;
    this.m_isCreationRunning = true;
    this.Fire_CreateStarted();
    if (!this.m_creationParams.RequireAction(CApIVInterferenceVolumeCreationParams.eActions.DisplayBlankedSweptVolumes))
      return;
    this.DisplayBlankedObjects(this.m_creationParams.SweptVolumes);
  }

  private void PerformPostCreateActions()
  {
    this.m_isCreationRunning = false;
    this.m_creationParams = (CApIVInterferenceVolumeCreationParams) null;
    this.Fire_CreateFinished();
  }

  private TxInterferenceVolume PerformCreation(TxInterferenceVolumeCreationDataEx creationData)
  {
    TxInterferenceVolume interferenceVolume = (TxInterferenceVolume) null;
    if (creationData != null)
    {
      try
      {
        interferenceVolume = TxInterferenceVolumeEx.CreateInterferenceVolume(creationData);
      }
      catch (TxUnknownErrorException ex)
      {
        int num = (int) this.Fire_NotificationFeedback(this.GetNotification_CreateInterferenceVolumeFailed());
      }
    }
    return interferenceVolume;
  }

  private bool PerformCalculation(
    TxInterferenceVolume interferenceVolume,
    TxInterferenceVolumeCalculationData calculationData)
  {
    bool flag = false;
    if (TxInterferenceVolume.op_Inequality(interferenceVolume, (ITxObject) null))
    {
      if (calculationData != null)
      {
        try
        {
          interferenceVolume.Calculate(calculationData, this.m_mayProceedCallback);
          flag = true;
        }
        catch (TxNoIntersectionException ex)
        {
          switch (this.Fire_NotificationFeedback(this.GetNotification_MissingIntersectionForSweptVolumes()))
          {
            case CUiIVNotificationFeedbackEventArgs.eFeedbackResult.Yes:
              this.m_creationParams.InsertAction(CApIVInterferenceVolumeCreationParams.eActions.DeleteEmptyInterferenceVolume);
              flag = true;
              break;
            case CUiIVNotificationFeedbackEventArgs.eFeedbackResult.No:
              flag = true;
              break;
          }
        }
        catch (TxIntersectionAlgorithmFailedException ex)
        {
          int num = (int) this.Fire_NotificationFeedback(this.GetNotification_CalculateInterferenceVolumeFailed());
        }
        catch (TxUnknownErrorException ex)
        {
          int num = (int) this.Fire_NotificationFeedback(this.GetNotification_CalculateInterferenceVolumeFailed());
        }
        if (flag && this.m_isCreationAbortSubmitted)
          flag = false;
      }
    }
    return flag;
  }

  private TxInterferenceVolumeCreationDataEx GenerateCreationData()
  {
    TxInterferenceVolumeCreationDataEx creationData = (TxInterferenceVolumeCreationDataEx) null;
    if (this.m_creationParams != null && this.m_creationParams.SweptVolumes != null && this.m_creationParams.Name != null)
    {
      ITxSweptVolumeHoldingObject volumeHoldingObject1 = CApIVUtilities.GetSweptVolumeHoldingObject(((Collection<ITxObject>) this.m_creationParams.SweptVolumes)[0] as TxSweptVolume);
      ITxSweptVolumeHoldingObject volumeHoldingObject2 = CApIVUtilities.GetSweptVolumeHoldingObject(((Collection<ITxObject>) this.m_creationParams.SweptVolumes)[1] as TxSweptVolume);
      if (volumeHoldingObject1 != null && volumeHoldingObject2 != null)
      {
        TxObjectList txObjectList = new TxObjectList();
        ((Collection<ITxObject>) txObjectList).Add((ITxObject) volumeHoldingObject1);
        ((Collection<ITxObject>) txObjectList).Add((ITxObject) volumeHoldingObject2);
        creationData = new TxInterferenceVolumeCreationDataEx(this.m_creationParams.Name, txObjectList);
      }
    }
    return creationData;
  }

  private void GenerateCalculationData(ref TxInterferenceVolumeCalculationData resultData)
  {
    if (this.m_creationParams == null || this.m_creationParams.SweptVolumes == null)
      return;
    resultData = new TxInterferenceVolumeCalculationData(this.m_creationParams.SweptVolumes);
  }

  private bool DisplayBlankedObjects(TxObjectList displayableObjs)
  {
    bool flag = false;
    foreach (ITxDisplayableObject displayableObj in (Collection<ITxObject>) displayableObjs)
    {
      if (displayableObj.Visibility == 2)
      {
        flag = CApIVUtilities.DisplayObject(displayableObj);
        if (!flag)
          break;
      }
    }
    return flag;
  }

  private CUiIVNotification GetNotification_IdenticalSweptVolumes()
  {
    return new CUiIVNotification(eUiIVNotificationCode.CAN_CREATE_ERR_IDENTICAL_SWEPT_VOLUMES);
  }

  private CUiIVNotification GetNotification_IdenticalSweptVolumesHoldingObject()
  {
    eUiIVNotificationCode code = eUiIVNotificationCode.UNKNOWN;
    List<string> stringParams = new List<string>(1);
    ITxSweptVolumeHoldingObject volumeHoldingObject = CApIVUtilities.GetSweptVolumeHoldingObject(((Collection<ITxObject>) this.m_dataProvider.IVData_SweptVolumes)[0] as TxSweptVolume);
    if (CApIVUtilities.IsOperation((ITxObject) volumeHoldingObject))
    {
      code = eUiIVNotificationCode.CAN_CREATE_ERR_IDENTICAL_INTERFERENCE_VOLUME_OPERATION_HOLDERS;
      stringParams.Add(((ITxObject) volumeHoldingObject).Name);
    }
    else if (CApIVUtilities.IsRoboticProgram((ITxObject) volumeHoldingObject))
    {
      code = eUiIVNotificationCode.CAN_CREATE_ERR_IDENTICAL_INTERFERENCE_VOLUME_PROGRAM_HOLDERS;
      stringParams.Add(((ITxObject) volumeHoldingObject).Name);
    }
    return new CUiIVNotification(code, stringParams);
  }

  private CUiIVNotification GetNotification_ExistingInterferenceVolume()
  {
    return new CUiIVNotification(eUiIVNotificationCode.CAN_CREATE_WARNING_INTERFERENCE_VOLUME_ALREADY_EXIST, new List<string>(2)
    {
      ((ITxObject) CApIVUtilities.GetSweptVolumeHoldingObject(((Collection<ITxObject>) this.m_dataProvider.IVData_SweptVolumes)[0] as TxSweptVolume)).Name,
      ((ITxObject) CApIVUtilities.GetSweptVolumeHoldingObject(((Collection<ITxObject>) this.m_dataProvider.IVData_SweptVolumes)[1] as TxSweptVolume)).Name
    });
  }

  private CUiIVNotification GetNotification_EmptyInterferenceVolumes()
  {
    TxObjectList dataSweptVolumes = this.m_dataProvider.IVData_SweptVolumes;
    TxSweptVolume sweptVolume1 = ((Collection<ITxObject>) dataSweptVolumes)[0] as TxSweptVolume;
    TxSweptVolume sweptVolume2 = ((Collection<ITxObject>) dataSweptVolumes)[1] as TxSweptVolume;
    bool flag1 = CApIVUtilities.IsEmptySweptVolume(sweptVolume1);
    bool flag2 = CApIVUtilities.IsEmptySweptVolume(sweptVolume2);
    eUiIVNotificationCode code = eUiIVNotificationCode.UNKNOWN;
    List<string> stringParams = new List<string>(2);
    if (flag1 & flag2)
    {
      stringParams.Add(sweptVolume1.Name);
      stringParams.Add(sweptVolume2.Name);
      code = eUiIVNotificationCode.CAN_CREATE_WARNING_ALL_SWEPT_VOLUMES_MISSING_GEOMETRY;
    }
    else if (flag1)
    {
      stringParams.Add(sweptVolume1.Name);
      code = eUiIVNotificationCode.CAN_CREATE_WARNING_SWEPT_VOLUME_MISSING_GEOMETRY;
    }
    else if (flag2)
    {
      stringParams.Add(sweptVolume2.Name);
      code = eUiIVNotificationCode.CAN_CREATE_WARNING_SWEPT_VOLUME_MISSING_GEOMETRY;
    }
    return new CUiIVNotification(code, stringParams);
  }

  private CUiIVNotification GetNotification_CreateInterferenceVolumeFailed()
  {
    return new CUiIVNotification(eUiIVNotificationCode.CREATE_ERR_INTERFERENCE_VOLUME_CREATE_FAIL);
  }

  private CUiIVNotification GetNotification_CalculateInterferenceVolumeFailed()
  {
    return new CUiIVNotification(eUiIVNotificationCode.CREATE_ERR_INTERFERENCE_VOLUME_CALCULATE_FAIL);
  }

  private CUiIVNotification GetNotification_BlankedSweptVolumes()
  {
    return new CUiIVNotification(eUiIVNotificationCode.CAN_CREATE_WARNING_BLANKED_SWEPT_VOLUMES);
  }

  private CUiIVNotification GetNotification_MissingIntersectionForSweptVolumes()
  {
    return new CUiIVNotification(eUiIVNotificationCode.CREATE_WARNING_INTERFERENCE_VOLUME_MISSING_INTERSECTION);
  }

  private void Fire_CreateStarted()
  {
    if (this.CreateStarted == null)
      return;
    this.CreateStarted((object) this, new EventArgs());
  }

  private void Fire_CreateFinished()
  {
    if (this.CreateFinished == null)
      return;
    this.CreateFinished((object) this, new EventArgs());
  }

  private CUiIVNotificationFeedbackEventArgs.eFeedbackResult Fire_NotificationFeedback(
    CUiIVNotification notification)
  {
    CUiIVNotificationFeedbackEventArgs.eFeedbackResult eFeedbackResult = CUiIVNotificationFeedbackEventArgs.eFeedbackResult.None;
    if (this.m_notificationFeedback != null)
    {
      CUiIVNotificationFeedbackEventArgs args = new CUiIVNotificationFeedbackEventArgs(notification);
      this.m_notificationFeedback((object) this, args);
      eFeedbackResult = args.FeedbackResult;
    }
    return eFeedbackResult;
  }

  private bool MayProceed(int i) => true;

  internal delegate void CreateStartedEventHandler(object sender, EventArgs args);

  internal delegate void CreateFinishedEventHandler(object sender, EventArgs args);
}
